package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://www.cutv.com/ 城市联合网络电视台
 * @author allen
 */
public class CutvNewsAnalyse implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.cutv.com/.*/[0-9]{4}-[0-9].*-[0-9].*/.*.shtml",
				"http://[a-z]*.cutv.com/.*/[0-9]*/[0-9]*.shtml",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

			title =doc.select("h1").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("h2").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("div.content_left_header").text();
				}
			}
			content=HtmlCleaner.getContentHtml(url, doc.select("div.wrap > div.main > div.text_box > p"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url, doc.select("div.main div.text_box"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url, doc.select("div.con_content_txt"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url, doc.select("div.content_left_center"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url, doc.select("div.article-text"));
			}

			String tempdate=doc.select("div.text_infro").text();
			date=DateUtils.matchDate(tempdate);
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.con_content_info").text());
			}
			if(date==null){
				date=DateUtils.matchDate(url);
			}

			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
			/**
			 * 解析用于更新的地址
			 */
			String author=doc.select("div.con_topic_txt").text();
			if(author!=null){
				author=StringUtils.substringAfter(author, "来自于：");
				author=author.replace("收起", "");
			}
			if(StringUtils.isBlank(author)){
				author=doc.select("div.main div.text_box div.text_infro small").text();
				author=author.replace("出处：", "");
			}
			if(StringUtils.isBlank(author)){
				author=doc.select("div.con_content_info").text();
				author=StringUtils.substringBetween(author, "来源：", "评论数：");
			}
			news.setAuthor(author);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {

		return null;
	}
	public static void main(String[] args) {
		CutvNewsAnalyse a=new CutvNewsAnalyse();
		String url="http://sztv.cutv.com/caijing/video/201510/13149882513.shtml";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
